package zhihui.mobile.manager.shop;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.cache.disc.impl.LimitedAgeDiskCache;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import zhihui.mobile.bean.shop.SkuInfo;
import zhihui.mobile.bean.shop.order.AddOrderInfo;
import zhihui.mobile.bean.shop.order.ShopOrder;
import zhihui.mobile.bean.shop.order.ShopOrderDetail;
import zhihui.mobile.config.Config;
import zhihui.mobile.interfaces.net.IHttpCallBackListener;
import zhihui.mobile.manager.IManager;
import zhihui.mobile.manager.user.UserManager;
import zhihui.mobile.net.HttpUtils;

/**
 * Created by 张公铭 on 2017/8/31 0031.
 */

public class ShopOrderManager implements IManager
{
    private static ShopOrderManager manager;
    public int timeover = 0;        //超时时间
    private List<ShopOrder> orderList = new ArrayList<>();  //订单列表
    private ShopOrderDetail curOrderDetail = null;
    private List<SkuInfo> confirmList = new ArrayList<>();  //确定订单的商品列表

    private synchronized static void created()
    {
        if (manager == null)
        {
            manager = new ShopOrderManager();
        }
    }

    public static ShopOrderManager getInstance()
    {
        if (manager == null)
        {
            created();
        }
        return manager;
    }

    public void setCurOrderDetail(ShopOrderDetail curOrderDetail)
    {
        this.curOrderDetail = curOrderDetail;
    }

    public ShopOrderDetail getCurOrderDetail()
    {
        return curOrderDetail;
    }

    public List<ShopOrder> getOrderList()
    {
        return orderList;
    }

    public void setOrderList(List<ShopOrder> orderList)
    {
        if (this.orderList == null)
        {
            this.orderList = new ArrayList<>();
        }
        this.orderList.clear();
        this.orderList.addAll(orderList);
    }

    public void setConfirmList(List<SkuInfo> list)
    {
        if (confirmList == null)
        {
            confirmList = new ArrayList<>();
        }
        confirmList.clear();
        confirmList.addAll(list);
    }

    public List<SkuInfo> getConfirmList()
    {
        return confirmList;
    }

    /**
     * 获取订单列表
     */
    public void getShopOrders(IHttpCallBackListener listener, String state)
    {
        Map<String, String> params = new HashMap<>();
        params.put("service", "org.zhmm.app.api.order.Order");
        params.put("method", "orderList");

        List<Object> list = new ArrayList<>();
        list.add(state);
        list.add(UserManager.getInstance().getCurUser().getId());
        HttpUtils.postAes(Config.URL, params, list, listener);
    }

    /**
     * 添加订单
     */
    public void addShopOrder(IHttpCallBackListener listener, AddOrderInfo orderInfo)
    {
        Map<String, String> params = new HashMap<>();
        params.put("service", "org.zhmm.app.api.order.Order");
        params.put("method", "create");

        List<Object> list = new ArrayList<>();
        list.add(orderInfo.getDiscountId());
        list.add(UserManager.getInstance().getCurUser().getId());
        list.add(orderInfo.getAddressId());
        list.add(orderInfo.getCityCode());
        list.add(orderInfo.getDispId());
        list.add(orderInfo.getOrderType());
        list.add(orderInfo.getCoupon_money());
        list.add(orderInfo.getSalesId());
        Gson gson = new Gson();
        String json = gson.toJson(orderInfo.getList());
        list.add(json);

        HttpUtils.postAes(Config.URL, params, list, listener);
    }

    /**
     * 获取订单详情
     */
    public void getOrderDetail(IHttpCallBackListener listener, String orderId)
    {
        Map<String, String> params = new HashMap<>();
        params.put("service", "org.zhmm.app.api.order.Order");
        params.put("method", "findOrderDetail");

        List<Object> list = new ArrayList<>();
        list.add(orderId);
        HttpUtils.postAes(Config.URL, params, list, listener);
    }

    /**
     * 修改订单状态
     */
    public void updateOrderStatus(IHttpCallBackListener listener, String orderId, int status)
    {
        Map<String, String> params = new HashMap<>();
        params.put("service", "org.zhmm.app.api.order.Order");
        params.put("method", "updateOrderStatus");

        List<Object> list = new ArrayList<>();
        list.add(orderId);
        list.add(status);
        HttpUtils.postAes(Config.URL, params, list, listener);
    }

    /**
     * 修改订单状态
     */
    public void updateOrderListStatus(String orderId, int status)
    {
        List<ShopOrder> list = new ArrayList<>();
        for (ShopOrder order : orderList)
        {
            if (order.getOrderId().equals(orderId))
            {
                order.setStatus(status);
            }
            list.add(order);
        }
        orderList.clear();
        orderList.addAll(list);
    }

    @Override
    public void clear()
    {

    }
}
